#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "Car.h"

//创建一个车辆信息结构体
Car* Car_Create() {
    Car* C = (Car*)malloc(sizeof(Car));
    C->in_time = -1;
    C->out_time = -1;
    C->costs = 0.0;
    return C;
}

//计算车辆的收取费用并输出费用
double Car_Bill(Car *C) {
    double seconds = difftime(C->out_time, C->in_time);
    C->costs = seconds / 300.0; //每停留5分钟计费1元
    return (C->costs);
}

//修改车辆车牌号
Car* Car_ModifyL(Car *C, char* plate) {
    strcpy(C->license_plate,plate);
    return C;
}

//修改车辆进入时间
Car* Car_ModifyI(Car* C, time_t t) {
    C->in_time = t;
    return C;
}

//修改车辆出去时间
Car* Car_ModifyO(Car* C, time_t t) {
    C->out_time = t;
    return C;
}

//修改车辆计费
Car* Car_ModifyC(Car* C, double cost) {
    C->costs = cost;
    return C;
}

//输出车辆的信息，存在返回1，没有返回0
int Car_Check(Car* C) {
    if (C == NULL) {
        return 0;
    }
    else {
        struct tm *inT=NULL, *outT=NULL, *nowT=NULL;
        if (C->in_time == -1)
            inT = localtime(&C->in_time);
        if (C->out_time == -1)
            outT = localtime(&C->out_time);

        time_t now_time;
        time(&now_time);
        nowT = localtime(&now_time);

        if (inT != NULL && outT == NULL) {
            printf("车牌号：%s/n", C->license_plate);

            printf("进入时间：");
            printf("%d月", inT->tm_mon + 1);
            printf("%d日", inT->tm_mday);
            printf("%d时", inT->tm_hour);
            printf("%d分", inT->tm_min);
            printf("%d秒\n", inT->tm_sec);

            printf("出去时间：暂无\n");

            printf("停留时间：");
            printf("%d月", (nowT->tm_mon + 1) - (inT->tm_mon + 1));
            printf("%d日", nowT->tm_mday - inT->tm_mday);
            printf("%d时", nowT->tm_hour - inT->tm_hour);
            printf("%d分", nowT->tm_min - inT->tm_min);
            printf("%d秒\n", nowT->tm_sec - inT->tm_sec);

            printf("计费：");
            double seconds = difftime(now_time, C->in_time);
            C->costs = seconds / 300.0;
            printf("%.2f元\n", C->costs);
        }
        else if (inT != NULL && outT != NULL) {
            printf("车牌号：%s/n", C->license_plate);

            printf("进入时间：");
            printf("%d月", inT->tm_mon + 1);
            printf("%d日", inT->tm_mday);
            printf("%d时", inT->tm_hour);
            printf("%d分", inT->tm_min);
            printf("%d秒\n", inT->tm_sec);

            printf("出去时间：");
            printf("%d月", outT->tm_mon + 1);
            printf("%d日", outT->tm_mday);
            printf("%d时", outT->tm_hour);
            printf("%d分", outT->tm_min);
            printf("%d秒\n", outT->tm_sec);

            printf("停留时间：");
            printf("%d月", (outT->tm_mon + 1) - (inT->tm_mon + 1));
            printf("%d日", outT->tm_mday - inT->tm_mday);
            printf("%d时", outT->tm_hour - inT->tm_hour);
            printf("%d分", outT->tm_min - inT->tm_min);
            printf("%d秒\n", outT->tm_sec - inT->tm_sec);

            printf("计费：");
            C->costs = Car_Bill(C);
            printf("%.2f元\n", C->costs);
        }
        return 1;
    }

}